package com.it.controller;

import com.it.bean.Fruit;
import com.it.service.FruitService;
import com.it.utils.StringUtil;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;


public class FruitController {

    public FruitService fruitService;

    /**
     * 删除
     *
     * @param request
     * @throws IOException
     */
    public String delete(HttpServletRequest request) throws Exception {
        String id = request.getParameter("id");
        if (StringUtil.isNotEmpty(id)) {
            fruitService.delFruit(Integer.parseInt(id));
            return "fruit.do";
        }
        return null;


    }

    /**
     * 更新
     *
     * @param request
     */
    public String update(HttpServletRequest request) throws Exception {
        String fid = request.getParameter("fid");
        String fname = request.getParameter("fname");
        String price = request.getParameter("price");
        String fcount = request.getParameter("fcount");
        String remark = request.getParameter("remark");
        Fruit f = new Fruit(Integer.parseInt(fid), fname, Integer.parseInt(price), Integer.parseInt(fcount), remark);
        fruitService.updateFruit(f);
        //   response.sendRedirect("fruit.do");
        return "fruit.do";
    }

    /**
     * 编辑
     *
     * @param request
     */
    public String edit(HttpServletRequest request) throws Exception {
        String fid = request.getParameter("fid");
        if (StringUtil.isNotEmpty(fid)) {
            Fruit fruit = fruitService.getFruitByFid(Integer.parseInt(fid));
            request.setAttribute("fruit", fruit);
            return "edit";
        }
        return null;

    }

    /**
     * 新增
     *
     * @param request
     */
    public String add(HttpServletRequest request) throws Exception {
        String fname = request.getParameter("fname");
        String price = request.getParameter("price");
        String fcount = request.getParameter("fcount");
        String remark = request.getParameter("remark");
        Fruit f = new Fruit(fname, Integer.parseInt(price), Integer.parseInt(fcount), remark);
        fruitService.addFruit(f);
        return "fruit.do";


    }

    /**
     * post查询
     *
     * @param request
     */
    public String queryByPost(HttpServletRequest request) throws Exception {
        String pageStr = request.getParameter("pageNo");
        int pageNo = 1;
        if (StringUtil.isNotEmpty(pageStr)) {
            pageNo = Integer.parseInt(pageStr);
        }
        String queryParem = "";
        String keyword = request.getParameter("keyword");
        if (StringUtil.isNotEmpty(keyword)) {
            queryParem = keyword;
            pageNo = 1;
            request.getSession().setAttribute("queryParem", queryParem);
        } else {
            request.getSession().setAttribute("queryParem", "");
        }
        int count = fruitService.getFruitCount(queryParem);
        request.setAttribute("count", count);
        request.setAttribute("pageCount", (count + 4) / 5);
        List<Fruit> list = fruitService.getFruitList(pageNo, queryParem);
        request.setAttribute("list", list);
        request.setAttribute("pageNo", pageNo);
        return "index";
    }

    /**
     * get查询
     *
     * @param request
     */
    public String queryByGet(HttpServletRequest request) throws Exception {
        String pageStr = request.getParameter("pageNo");
        int pageNo = 1;
        if (StringUtil.isNotEmpty(pageStr)) {
            pageNo = Integer.parseInt(pageStr);
        }
        String queryParem = (String) request.getSession().getAttribute("queryParem");
        if (StringUtil.isEmpty(queryParem)) {
            queryParem = "";
        }
        int count = fruitService.getFruitCount(queryParem);
        request.setAttribute("count", count);
        request.setAttribute("pageCount", (count + 4) / 5);
        List<Fruit> list = fruitService.getFruitList(pageNo, queryParem);
        request.setAttribute("list", list);
        request.setAttribute("pageNo", pageNo);
        return "index";

    }
}
